package com.sunshine.lnuplus.ui.activity

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.widget.SeekBar
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.adapter.TimetableThemeAdapter
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.callback.RecyclerViewCallBack
import com.sunshine.lnuplus.utils.BackgroundUtils
import com.sunshine.lnuplus.utils.StatusBarUtils
import com.sunshine.lnuplus.utils.TagHelper
import com.sunshine.lnuplus.view.DialogWithoutView
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.activity_timetable_theme.*
import java.io.File

class TimetableThemeActivity : BaseActivity() {

    private lateinit var appSetting: SharedPreferences
    private lateinit var edit: SharedPreferences.Editor

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_timetable_theme)

        appSetting = getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE)
        edit = appSetting.edit()
        initView()
    }

    private fun initView() {
        StatusBarUtils.setStatusBarWithColor(this, color)
        text_mohu.text = "背景图片模糊度：${appSetting.getInt("background_mohu", 0)}%"
        seekBar_mohu.progress = appSetting.getInt("background_mohu", 0)

        Toasty.info(this, "左右滑动选择背景样式").show()
        title = when(getTimetableTheme()) {
            1 -> "已选择：纯白"
            2 -> "已选择：默认背景"
            3 -> "已选择：每日一图"
            4 -> "已选择：自定义"
            else -> "出错"
        }
        ultra_viewpager.adapter = TimetableThemeAdapter(this, object : RecyclerViewCallBack{
            override fun onChanged(position: Int) {
                setResult(TagHelper.TIMETABLETHEME)
                title = when(position) {
                    0 -> "已选择：纯白"
                    1 -> "已选择：默认背景"
                    2 -> "已选择：每日一图"
                    3 -> "正在选择：自定义"
                    else -> "出错"
                }
                if(position == 3){
                    DialogWithoutView.newInstance(
                        "请注意",
                        resources.getString(R.string.load_image_warning),
                        false,
                        DialogInterface.OnClickListener { _, _ ->
                            val intent = Intent(Intent.ACTION_GET_CONTENT)
                            intent.type = "image/*"
                            intent.addCategory(Intent.CATEGORY_OPENABLE)
                            startActivityForResult(intent, TagHelper.APPSETTING)
                        },
                        null).show(supportFragmentManager, "load_warn")

                } else {
                    Toasty.success(this@TimetableThemeActivity, "设置成功").show()
                    edit.putInt("timetable_theme", position + 1)
                }
            }
        })
        ultra_viewpager.currentItem = getTimetableTheme()!! - 1

        seekBar_mohu.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                text_mohu.text = "背景图片模糊度：${progress}%"
                edit.putInt("background_mohu", progress)
                setResult(TagHelper.TIMETABLETHEME)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                Toasty.success(this@TimetableThemeActivity, "设置成功").show()
            }

        })
    }

    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == TagHelper.APPSETTING && resultCode == RESULT_OK) {
            val imageUri: Uri?
            var imageFile: File? = null
            val uri = data!!.data // 获取图片的uri
            val intent_gallery_crop = Intent("com.android.camera.action.CROP")
            intent_gallery_crop.setDataAndType(uri, "image/*")
            intent_gallery_crop.putExtra("crop", "true")
            intent_gallery_crop.putExtra("scale", true)
            intent_gallery_crop.putExtra("aspectX", 9)
            intent_gallery_crop.putExtra("aspectY", 16)
            intent_gallery_crop.putExtra("return-data", false)
            // 创建文件保存裁剪的图片
            imageFile = BackgroundUtils.userBackgroundToFile(imageFile, applicationContext)
            imageUri = Uri.fromFile(imageFile)
            if (imageUri != null) {
                intent_gallery_crop.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
                intent_gallery_crop.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString())
            }
            startActivityForResult(intent_gallery_crop, TagHelper.APPSETTING2)
        }
        if (requestCode == TagHelper.APPSETTING2 && resultCode == RESULT_OK) {
            edit.putInt("timetable_theme", 4)
            title = "已选择：自定义"
            Toasty.success(this, "设置成功", Toasty.LENGTH_SHORT).show()
        }
    }

    override fun onPause() {
        super.onPause()
        edit.apply()
    }
}
